package com.liangtian.mdm.system.button.controller;

import com.alibaba.fastjson.JSON;
import com.liangtian.mdm.base.controller.BaseController;
import com.liangtian.mdm.base.utils.ResultObject;
import com.liangtian.mdm.base.utils.ShiroSessionUtils;
import com.liangtian.mdm.system.button.service.SysButtonService;
import com.liangtian.mdm.system.log.SysLogUtil;
import com.liangtian.mdm.system.log.model.SysLogEnum;
import com.liangtian.mdm.system.log.service.SysLogService;
import com.liangtian.mdm.system.ugroup.model.SysUgroup;
import com.liangtian.mdm.system.ugroup.service.SysUgroupService;
import com.liangtian.mdm.system.user.model.SysUser;
import com.liangtian.mdm.system.user.service.SysUserService;
import com.liangtian.mdm.util.ConstantBean;
import com.liangtian.mdm.util.RequestUtils;
import com.liangtian.mdm.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>Encoding    :  UTF-8</p>
 * <p>Package Name: com.liangtian.mdm.system.button.controller </p>
 * <p>Project Name: dm </p>
 * <p>Copyright   :  Copyright(c) 2016 石化盈科信息技术有限责任公司 </p>
 * <p>Created Time: 2016-10-13</p>
 * <p>Description : </p>
 *
 * @author : byr
 * @version :  1.0
 */
@Controller
@RequestMapping("/sysButton")
public class SysButtonController extends BaseController {

    //注入服务接口
    @Autowired
    SysButtonService sysButtonServiceImpl;

    //日志服务接口对象
    @Autowired
    SysLogService sysLogServiceImpl;

    //用户接口对象
    @Autowired
    SysUserService sysUserServiceImpl;

    @Autowired
    SysUgroupService sysUgroupService;

    //当前用户
    ////@Value("${ConstantBean.CURRENT_SESSION_USER}")
    //private String ConstantBean.CURRENT_SESSION_USER;

    /*
     * @param Map 对象
     * @return
     * @desc 获取全部数据
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/queryAllButtonList", method = RequestMethod.POST)
    @ResponseBody
    public String queryAllButtonList(HttpServletRequest request) {  //JSONObject
        //查询条件
        String rst = null;
        Map<String, Object> param = RequestUtils.paramToMap(request);
        System.out.println("=========queryList==========");
        System.out.println(JSON.toJSONString(param));
        Map<String, Object> resMap = sysButtonServiceImpl.queryButtonList(param);
        rst = JSON.toJSONString(resMap);
        return rst;
    }


    /*
     * @param Map 对象
     * @return
     * @desc 获取全部数据
     */
    @RequestMapping(value = "/saveButtonList", method = RequestMethod.POST)
    @ResponseBody
    public ResultObject insertButtonsPermission(HttpServletRequest request) {  //JSONObject
        //查询条件
        boolean ret = false;
        Map<String, Object> param = new HashMap<>();
        String[] pages = request.getParameterValues("pages");
        String[] buttons = request.getParameterValues("buttons");
        String survey = request.getParameter("surveyType");
        String userId = request.getParameter("userId");
        String uGroupId = request.getParameter("uGroupId");

        String logContent = null;
        if (!StringUtil.isBlank(userId) && !("null".equals(userId))) {
            param.put("userId", userId);
            SysUser sysUser = new SysUser();
            sysUser.setUserId(Long.valueOf(userId));
            SysUser user2 = sysUserServiceImpl.selectUserById(sysUser);
            logContent = "用户【" + user2.getUserName() + "】";
        }
        if (!StringUtil.isBlank(uGroupId) && !("null".equals(uGroupId))) {
            param.put("uGroupId", uGroupId);
            SysUgroup ugroup = sysUgroupService.queryOne(uGroupId);
            logContent = "用户组【" + ugroup.getGroupname() + "】";
        }
        param.put("surveyType", survey);
        param.put("pages", pages);
        param.put("buttons", buttons);
        System.out.println("=========queryList==========");
        System.out.println(JSON.toJSONString(param));
        //获取session中用户
        SysUser user = ShiroSessionUtils.getSessionSysUser(ConstantBean.CURRENT_SESSION_USER);
       
        /*SysLog sysLog = new SysLog();
        Date date = new Date();
        sysLog.setUserName(user.getUserName());
        sysLog.setBrowserType(BrowserUtils.getBrowserName(request));
        sysLog.setOperationDate(date);*/
        try {
            ret = sysButtonServiceImpl.insertButtons(param);
            SysLogUtil.insertLog("为" + logContent + "新增按钮权限",
                    SysLogEnum.SYS_PERMISSION_BUTTON.getCode(), SysLogEnum.XZ.getCode(), request);
        	
           /*if (ret) {
                sysLog.setOperationContent("用户【"+user.getUserName()+"】于"+ DateUtils.dateToString(date,"yyyy-MM-dd HH:mm:ss")+"为用户【"+user2.getUserName()+"】新增按钮权限成功");
           }
            //成功记录操作日志
            sysLogServiceImpl.insertLog(sysLog);*/
        } catch (Exception e) {
           /* sysLog.setOperationContent("用户【"+user.getUserName()+"】于"+ DateUtils.dateToString(date,"yyyy-MM-dd HH:mm:ss")+"为用户【"+user2.getUserName()+"】新增按钮权限失败");
            //失败记录操作日志
            sysLogServiceImpl.insertLog(sysLog);*/
            e.printStackTrace();
        }
        int f = ret ? 1 : 0;
        ResultObject result = new ResultObject(f, "新增成功", "新增失败");
        return result;
    }

}
